/*
 * --------------------------------------------------------------------
 * @name FrameWelcome.java
 * @author Juan Camilo Ibarra
 *
 * This work is licensed under a Creative
 * Commons Attribution 3.0 License. This means you can to copy, 
 * distribute, transmit and adapt the work, under the following condition:
 * You must attribute the work in the manner specified by the author or licensor.
 *
 * Juan Camilo Ibarra	-	juanibarral@gmail.com
 * Oct 18, 2011
 * --------------------------------------------------------------------
 */

package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import util.MailSender;

public class FrameMain extends JFrame implements ActionListener{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private final int width = 300;
	private final int height = 300;
	private final String ACTION_BUTTON_FACIAL = "ACTION_BUTTON_FACIAL";
	private final String ACTION_BUTTON_CORPORAL = "ACTION_BUTTON_CORPORAL";
	
	private JButton buttonFacialTreatment;
	private JButton buttonCorporalTreatment;
	private FrameFacialSelection frameFacialSelection;
	
	private Dimension screenSize;
	
	private MailSender mailSender;
	
	public FrameMain()
	{
		mailSender = new MailSender();
		try {
			mailSender.configure("./ext/mail.config");
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage(), e.getClass().toString(), JOptionPane.ERROR_MESSAGE);
		}
		init();
	}
	
	private void init()
	{
		this.setTitle("Welcome");
		this.setSize(width, height);
		this.setLayout(new BorderLayout());
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setResizable(false);
		
		screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		this.setLocation((screenSize.width - width)/2, (screenSize.height - height)/2);
		
		buttonCorporalTreatment = new JButton("Tratamiento Corporal");
		buttonCorporalTreatment.setActionCommand(ACTION_BUTTON_CORPORAL);
		buttonCorporalTreatment.addActionListener(this);
		
		buttonFacialTreatment = new JButton("Tratamiento Facial");
		buttonFacialTreatment.setActionCommand(ACTION_BUTTON_FACIAL);
		buttonFacialTreatment.addActionListener(this);
		
		JPanel buttons = new JPanel(new GridLayout(2,1));
		buttons.add(buttonFacialTreatment);
		buttons.add(buttonCorporalTreatment);
		
		this.add(buttons);
	}


	@Override
	public void actionPerformed(ActionEvent actionEvent) {
		String actionCommand = actionEvent.getActionCommand();
		
		if(actionCommand.equals(ACTION_BUTTON_FACIAL))
		{
			frameFacialSelection = new FrameFacialSelection();
			frameFacialSelection.init(this);
		}
		else if(actionCommand.equals(ACTION_BUTTON_CORPORAL))
		{
			JOptionPane.showMessageDialog(this, "Corporal treatments");
		}
		
	}
	
	public Dimension getScreenSize()
	{
		return screenSize;
	}
	
	public void sendMail(String to, String filepath)
	{
		try {
			mailSender.sendMail(to);
			//TODO: Send the mail with the attachment
//			mailSender.sendMailWithAtt(to, filepath);
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, e.getMessage(), e.getClass().toString(), JOptionPane.ERROR_MESSAGE);
		}

	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FrameMain instance = new FrameMain();
		instance.setVisible(true);

	}


}
